﻿// My methods here.
//-----------------------------------------------------------------------
// <Copyright file=LoaiKhuyenMai company=DHPD>
// Copyright (c) DHPD Corporation (http://dhpd.edu.vn). All rights reserved.
// </copyright>
// <author>HP</author>
//-----------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KM.DAT;
using KM.ENT;
using KM.Models;

namespace KM.DAT
{
    /// <summary>
    /// This is datLoaiKhuyenMai class
    /// </summary>

    public class datLoaiKhuyenMai
    {
        public datLoaiKhuyenMai()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        SqlCommand cmd;
        SqlDataReader dr;
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        #region  === tạo mới và sửa LoaiKhuyenMai===
        public void createEdit(entLoaiKhuyenMai entLKM)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiKhuyenMaiCreateEdit", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("LoaiKhuyenMaiId", entLKM.LoaiKhuyenMaiId);
            cmd.Parameters.AddWithValue("TenLoaiKhuyenMai", entLKM.TenLoaiKhuyenMai);

            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion

        #region  === getall LoaiKhuyenMai===
        public List<entLoaiKhuyenMai> getAll(ref PagingInfoModel page)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiKhuyenMaiGetAll", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
            cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
            cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
            dr = cmd.ExecuteReader();
            entLoaiKhuyenMai entLKM = new entLoaiKhuyenMai();
            List<entLoaiKhuyenMai> lst = new List<entLoaiKhuyenMai>();
            while (dr.Read())
            {
                entLKM = new entLoaiKhuyenMai();
                entLKM.LoaiKhuyenMaiId = Convert.ToInt32(dr["LoaiKhuyenMaiId"].ToString());
                entLKM.TenLoaiKhuyenMai = dr["TenLoaiKhuyenMai"].ToString();

                lst.Add(entLKM);
            }

            dr.Close();
            if (lst.Count > 0)
            {
                // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
                page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            }

            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall LoaiKhuyenMai===
        public List<entLoaiKhuyenMai> getAll()
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiKhuyenMaiGetAll", sqlConnection);
           
            dr = cmd.ExecuteReader();
            entLoaiKhuyenMai entLKM = new entLoaiKhuyenMai();
            List<entLoaiKhuyenMai> lst = new List<entLoaiKhuyenMai>();
            while (dr.Read())
            {
                entLKM = new entLoaiKhuyenMai();
                entLKM.LoaiKhuyenMaiId = Convert.ToInt32(dr["LoaiKhuyenMaiId"].ToString());
                entLKM.TenLoaiKhuyenMai = dr["TenLoaiKhuyenMai"].ToString();

                lst.Add(entLKM);
            }

            dr.Close();

            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall LoaiKhuyenMai by chaid===
        public List<entLoaiKhuyenMai> getAllByChaId(int id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiKhuyenMaiGetAllByChaid", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@LoaiKhuyenMaiChaId", id);
            dr = cmd.ExecuteReader();
            entLoaiKhuyenMai entLKM = new entLoaiKhuyenMai();
            List<entLoaiKhuyenMai> lst = new List<entLoaiKhuyenMai>();
            while (dr.Read())
            {
                entLKM = new entLoaiKhuyenMai();
                entLKM.LoaiKhuyenMaiId = Convert.ToInt32(dr["LoaiKhuyenMaiId"].ToString());
                entLKM.TenLoaiKhuyenMai = dr["TenLoaiKhuyenMai"].ToString();

                lst.Add(entLKM);
            }

            dr.Close();

            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall LoaiKhuyenMai===
        public List<entLoaiKhuyenMai> getAllByForeignKey(int Id, ref PagingInfoModel page)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiKhuyenMaiGetAllByForeignKey", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("Id", Id);
            cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
            cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
            cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
            dr = cmd.ExecuteReader();
            entLoaiKhuyenMai entLKM = new entLoaiKhuyenMai();
            List<entLoaiKhuyenMai> lst = new List<entLoaiKhuyenMai>();
            while (dr.Read())
            {
                entLKM = new entLoaiKhuyenMai();
                entLKM.LoaiKhuyenMaiId = Convert.ToInt32(dr["LoaiKhuyenMaiId"].ToString());
                entLKM.TenLoaiKhuyenMai = dr["TenLoaiKhuyenMai"].ToString();

                lst.Add(entLKM);
            }

            dr.Close();
            if (lst.Count > 0)
            {
                // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
                page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            }

            sqlConnection.Close();
            return lst;
        }
        #endregion

        #region  === getinfo LoaiKhuyenMai===
        public entLoaiKhuyenMai getInfo(int LoaiKhuyenMaiId)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiKhuyenMaiGetInfo", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("LoaiKhuyenMaiId", DbType.Int32);
            sqla[0].Value = LoaiKhuyenMaiId;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entLoaiKhuyenMai entLKM = new entLoaiKhuyenMai();
            if (dr.Read())
            {
                entLKM.LoaiKhuyenMaiId = Convert.ToInt32(dr["LoaiKhuyenMaiId"].ToString());
                entLKM.TenLoaiKhuyenMai = dr["TenLoaiKhuyenMai"].ToString();
                entLKM.Keywords = dr["Keywords"].ToString();
                entLKM.Title = dr["Title"].ToString();
                entLKM.Description = dr["Description"].ToString();

            }
            dr.Close();
            sqlConnection.Close();
            return entLKM;
        }
        #endregion
        #region  === getinfo LoaiKhuyenMai===
        public entLoaiKhuyenMai getInfo(string tieude)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiKhuyenMaiGetInfoByTieude", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("tieude", DbType.String);
            sqla[0].Value = tieude;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entLoaiKhuyenMai entLKM = new entLoaiKhuyenMai();
            if (dr.Read())
            {
                entLKM.LoaiKhuyenMaiId = Convert.ToInt32(dr["LoaiKhuyenMaiId"].ToString());
                entLKM.TenLoaiKhuyenMai = dr["TenLoaiKhuyenMai"].ToString();
                entLKM.Keywords = dr["Keywords"].ToString();
                entLKM.Title = dr["Title"].ToString();
                entLKM.Description = dr["Description"].ToString();

            }
            dr.Close();
            sqlConnection.Close();
            return entLKM;
        }
        #endregion

        #region === xoa LoaiKhuyenMai===
        public void delete(int id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiKhuyenMaiDeleteById", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = id;

            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion

       
    }
}
